package com.hex.ds.hdtp.core.app.common.conf;

import com.hex.ds.hdtp.core.app.common.constant.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Package com.hex.ds.hdtp.core.app.common.conf
 * @ClassName AsyncTaskPoolConfig
 * @Description 各个任务线程池配置
 * @Author gj.xu
 * @Date 2024/3/4 14:21
 * @Version v2.0
 **/
@Configuration
@EnableAsync
public class AsyncTaskPoolConfig {

    /**
     * 脚本转换线程池--配置参数
     */
    private final ScriptConvertAsyncContants scriptConvertAsyncContants;

    /**
     * 脚本分析线程池--配置参数
     */
    private final ScriptAnalyzeAsyncContants scriptAnalyzeAsyncContants;

    /**
     * 脚本验证线程池--配置参数
     */
    private final ScriptVerifyAsyncContants scriptVerifyAsyncContants;

    /**
     * 脚本测试线程池--配置参数
     */
    private final ScriptTestAsyncContants scriptTestAsyncContants;

    /**
     * 数据迁移线程池--配置参数
     */
    private final DataTransferAsyncContants dataTransferAsyncContants;

    /**
     * 数据比对线程池配置-配置参数
     */
    private final DataContrastAsyncContants dataContrastAsyncContants;

    /**
     * 数据比对-明细比对线程池--配置参数
     */
    private final DataDetailContrastAsyncContants dataDetailContrastAsyncContants;

    @Autowired(required = false)
    public AsyncTaskPoolConfig(ScriptConvertAsyncContants scriptConvertAsyncContants,
                               ScriptAnalyzeAsyncContants scriptAnalyzeAsyncContants,
                               ScriptVerifyAsyncContants scriptVerifyAsyncContants,
                               DataContrastAsyncContants dataContrastAsyncContants,
                               DataTransferAsyncContants dataTransferAsyncContants,
                               ScriptTestAsyncContants scriptTestAsyncContants,
                               DataDetailContrastAsyncContants dataDetailContrastAsyncContants) {
        this.scriptConvertAsyncContants = scriptConvertAsyncContants;
        this.scriptAnalyzeAsyncContants = scriptAnalyzeAsyncContants;
        this.scriptVerifyAsyncContants = scriptVerifyAsyncContants;
        this.scriptTestAsyncContants = scriptTestAsyncContants;
        this.dataContrastAsyncContants = dataContrastAsyncContants;
        this.dataTransferAsyncContants = dataTransferAsyncContants;
        this.dataDetailContrastAsyncContants = dataDetailContrastAsyncContants;
    }

    /**
     * @Method dataTransferAsyncTaskExecutor
     * @Param
     * @Return java.util.concurrent.Executor
     * @Description 数据迁移线程池
     * @Author wz.li
     * @Date 2023/8/24 17:55
     * @Version V2.0
     */
    @Bean(name = "dataTransferAsyncTaskExecutor")
    public Executor dataTransferAsyncTaskExecutor() {
        return initExecutor(dataTransferAsyncContants, "dataTransferAsyncTaskExecutor-");
    }

    /**
     * @Method getDataContrastAsyncTaskExecutor
     * @Param
     * @Return java.util.concurrent.Executor
     * @Description 数据比对线程池<br>
     * @Author wz.li<br>
     * @Date 2023/8/16 18:27 <br>
     * @Version V2.0
     */
    @Bean(name = "dataContrastAsyncTaskExecutor")
    public Executor getDataContrastAsyncTaskExecutor() {
        return initExecutor(dataContrastAsyncContants, "dataContrastAsyncTaskExecutor-");
    }

    /**
     * @Method getDataDetailContrastAsyncTaskExecutor
     * @Param
     * @Return java.util.concurrent.Executor
     * @Description 数据明细比对线程池
     * @Author Liang Fuhao
     * @Date 2024/1/11 11:00
     * @Version V2.0
     */
    @Bean(name = "dataDetailContrastAsyncTaskExecutor")
    public Executor getDataDetailContrastAsyncTaskExecutor() {
        return initExecutor(dataDetailContrastAsyncContants, "dataDetailContrastAsyncTaskExecutor-");
    }

    /**
     * @Method scriptConvertExecutor
     * @Param
     * @Return java.util.concurrent.Executor
     * @Description 脚本转换线程池<br>
     * @Author wz.li<br>
     * @Date 2023/8/16 18:28 <br>
     * @Version V2.0
     */
    @Bean(name = "scriptConvertAsyncTaskExecutor")
    public Executor scriptConvertExecutor() {
        return initExecutor(scriptConvertAsyncContants, "scriptConvertAsyncTaskExecutor-");
    }

    /**
     * @Method scriptAnalyzeExecutor
     * @Param
     * @Return java.util.concurrent.Executor
     * @Description 脚本分析线程池
     * @Author gj.xu
     * @Date 2024/1/5 14:28
     * @Version V2.0
     */
    @Bean(name = "scriptAnalyzeAsyncTaskExecutor")
    public Executor scriptAnalyzeExecutor() {
        return initExecutor(scriptAnalyzeAsyncContants, "scriptAnalyzeAsyncTaskExecutor-");
    }

    /**
     * @Method scriptVerifyAsyncTaskExecutor
     * @Param
     * @Return java.util.concurrent.Executor
     * @Description 脚本验证线程池<br>
     * @Author wz.li<br>
     * @Date 2023/8/16 18:33 <br>
     * @Version V2.0
     */
    @Bean(name = "scriptVerifyAsyncTaskExecutor")
    public Executor scriptVerifyAsyncTaskExecutor() {
        return initExecutor(scriptVerifyAsyncContants, "scriptVerifyAsyncTaskExecutor-");
    }

    /**
     * @Method scriptTestAsyncTaskExecutor
     * @Param
     * @Return java.util.concurrent.Executor
     * @Description 脚本测试线程池
     * @Author wz.li
     * @Date 2023/9/1 15:59
     * @Version V2.0
     */
    @Bean(name = "scriptTestAsyncTaskExecutor")
    public Executor scriptTestAsyncTaskExecutor() {
        return initExecutor(scriptTestAsyncContants, "scriptTestAsyncTaskExecutor-");
    }

    /**
     * @Method initExecutor
     * @Param asyncContants
     * @Param prefix
     * @Return org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
     * @Description 根据配置初始化各个任务的线程池
     * @Author gj.xu
     * @Date 2024/3/4 14:26
     * @Version V2.0
     */
    private ThreadPoolTaskExecutor initExecutor(AsyncContants asyncContants, String prefix) {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(asyncContants.getCorePoolSize());
        threadPoolTaskExecutor.setMaxPoolSize(asyncContants.getMaxPoolSize());
        threadPoolTaskExecutor.setQueueCapacity(asyncContants.getQueueCapacity());
        threadPoolTaskExecutor.setKeepAliveSeconds(asyncContants.getKeepAliveSeconds());
        threadPoolTaskExecutor.setThreadNamePrefix(prefix);
        // 线程池对拒绝任务(无线程可用)的处理策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return threadPoolTaskExecutor;
    }
}
